package paytreasure.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import paytreasure.dao.IncomeDao;
import paytreasure.po.Consume;
import paytreasure.po.Income;
import paytreasure.service.IncomeService;
import paytreasure.util.ConstantUtil;
import paytreasure.vo.ResultVo;

public class IncomeServiceImpl implements IncomeService {
	
	@Autowired
	private IncomeDao incomeDao;
	
	private ResultVo result;
	
	public ResultVo saveIncome(Income income) {
		
		if(income.getCreateDate()==null){
			return new ResultVo(ConstantUtil.RESULT_ERROR, "添加失败！");
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
		String monthDate = sdf.format(income.getCreateDate());
		
		int count = incomeDao.getCount(monthDate);
		if(count > 0) {
			//查询已添加月份
			List<String> month = incomeDao.queryAllMonth();
			String string = "";
			for (String str : month) {
				string += str + ",";
			}
			string = string.substring(0,string.length()-1);
			result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "当前已添加",string);
			return result;
		}
		
		int i = incomeDao.insert(income);
		if (i == 0) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "数据保存异常！",new ArrayList<Object>());
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "保存成功！",new ArrayList<Object>());
		return result;
	}

	public ResultVo updateIncome(Income income) {
		// TODO Auto-generated method stub
		int i = incomeDao.update(income);
		if (i == 0) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR, "数据修改异常！");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "修改成功！");
		return result;
	}

	public ResultVo getById(String incomeId) {
		// TODO Auto-generated method stub
		Income income = incomeDao.get(incomeId);
		if( income == null) {
			result = new ResultVo(ConstantUtil.RESULT_ERROR,"未获取到数据");
			return result;
		}
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "获取成功", income);
		return result;
	}
	
	//月账单
	public ResultVo queryBymonth(HashMap<String, String> map) {
		//获取参数月份的 消费 收入 积分  钱来钱往
		Map<String,Object> mapAll = incomeDao.queryAll(map.get("month"));
		if(mapAll == null){
			mapAll = new HashMap<>();
		}
		mapAll.put("createYMonth", map.get("month"));
		//获取五个月的收入情况
		//List<Income> list = queryIncome();
		//获取五个月的收入情况
		List<Consume> list = queryConsume();
		mapAll.put("statistics", list);
		result = new ResultVo(ConstantUtil.RESULT_SUCCESS, "获取成功", mapAll);
		return result;
	}
	
	//5个月的消费情况
	public List<Income> queryIncome(){
		HashMap<String, String> dateMap = getMonth();
		return incomeDao.queryIncomeStatistics(dateMap);
	}
	
	//5个月的收入情况
	public List<Consume> queryConsume(){
		HashMap<String, String> dateMap = getMonth();
		return incomeDao.queryConsumeStatistics(dateMap);
	}
	
	//获取月份范围
	public HashMap<String, String> getMonth(){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.add(Calendar.MONTH, -4);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
		String minDate = sdf.format(calendar.getTime());
		String maxDate = sdf.format(new Date());
		HashMap<String, String> dateMap = new HashMap<String, String>();
		dateMap.put("minDate", minDate);
		dateMap.put("maxDate", maxDate);
		return dateMap;
	}

}
